package com.ruoyi.system.service.impl;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.constant.URLEnum;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.system.mpparam.ReqPoint;
import com.ruoyi.system.mpparam.ResPoint;
import com.ruoyi.system.service.IApiService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.ruoyi.common.constant.Constants.THIRD_URL;

@Service
public class ApiServiceImpl implements IApiService {

    private static String TOKEN = "";

    private Gson gson = new Gson();

    @Override
    public List<ResPoint> loadMapPoints(ReqPoint point) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("latitude", point.getLatitude());
        map.put("longitude", point.getLongitude());
        String result = sendHttp(URLEnum.LOAD_MAP_POINTS, map);
        Type type = new TypeToken<List<ResPoint>>(){}.getType();
        return gson.fromJson(result, type);
    }

    @Override
    public List<ResPoint> loadPointList(ReqPoint reqPoint) throws BusinessException {
        Map<String, Object> map = new HashMap<>();
        map.put("latitude", reqPoint.getLatitude());
        map.put("longitude", reqPoint.getLongitude());
        map.put("BelongType", reqPoint.getBelongType());
        map.put("PageIndex", reqPoint.getPageIndex());
        map.put("SortType", reqPoint.getSortType());
        map.put("Keyword", reqPoint.getKeyword());
        map.put("IsFree", reqPoint.getIsFree());
        map.put("FishType", reqPoint.getFishType());
        String result = sendHttp(URLEnum.LOAD_POINT_LIST, map);
        Type type = new TypeToken<List<ResPoint>>(){}.getType();
        return gson.fromJson(result, type);
    }

    private String sendHttp(URLEnum urlEnum, Map<String, Object> param) throws BusinessException {
        String url = THIRD_URL + urlEnum.getUrl();
        HttpRequest httpRequest = "get".equals(urlEnum.getMethod()) ? HttpRequest.get(url) : HttpRequest.post(url);
        httpRequest.body(JSON.toJSONString(param)).timeout(10 * 1000);
        if (urlEnum.isAuthentication()) {
//            tryGetToken();
            httpRequest.header(Header.AUTHORIZATION, "Bearer " + TOKEN);
        }
//        String result = httpRequest.execute().body();
//        return analysisResult(result, urlEnum, param);
        return httpRequest.execute().body();
    }

    private String analysisResult(String result, URLEnum urlEnum, Map<String, Object> param) throws BusinessException {
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray jsonArray = JSONArray.parseArray(result);
        boolean success = jsonObject.getBooleanValue("success");
        if (success) {
            return jsonObject.getString("data");
        } else {
            Integer status = jsonObject.getInteger("status");
            if (Objects.nonNull(status) && status.equals(401)) {
                TOKEN = "";
                //获取token重新发送请求
//                tryGetToken();
                return sendHttp(urlEnum, param);
            } else {
                throw new BusinessException(urlEnum.getUrl() + ",返回数据异常，" + jsonObject.getString("errorMessage"));
            }
        }
    }
}
